<template>
  <div class="groupAdd">
    <el-form ref="groupForm" :model="group" label-width="80px" :rules="rules">
      <el-form-item label="组名" prop="name">
        <el-input v-model="group.name"></el-input>
      </el-form-item>
      <el-form-item label="记号" prop="sourceId">
        <el-input v-model="group.sourceId"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" autosize v-model="group.remark"></el-input>
      </el-form-item>
      <el-form-item label="上级组织" prop="lastGroup">
        <el-select v-model="group.lastGroup" clearable  placeholder="请选择">
          <el-option
            v-for="item in allGroups"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="save" type="primary"
          ><span v-if="config.isChange">修改</span><span v-else>添加</span></el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'UserAdd',
  props: ["isChange", "fGroup"],
  data(){
      return{
          group:{
            id:null
            ,name:''
            ,sourceId:null
            ,remark:''
            ,lastGroup:null
          }
          ,config: {
            isChange: false
          }
          ,rules: {
            name: [{ required: true, message: "请输入组织名", trigger: "blur" }]
            ,remark: [{ required: true, message: "备注不能为空", trigger: "blur" }]
          }
          ,allGroups:[]
      }
  }
  ,watch: {
    isChange(val) {
      this.config.isChange = this.isChange;
    },
    fGroup(val) {
      if(this.fGroup==null){
        this.group={
              id:null
              ,name:''
              ,sourceId:null
              ,remark:''
              ,lastGroup:null
            };
      }else{this.group = JSON.parse(JSON.stringify(this.fGroup));}
    }
  }
  ,created(){
    if(this.fGroup==null){
      this.group={
            id:null
            ,name:''
            ,sourceId:null
            ,remark:''
            ,lastGroup:null
          };
    }else{this.group = JSON.parse(JSON.stringify(this.fGroup));}
    this.config.isChange = this.isChange;
    this.findAllGroups();
  }
  ,methods:{
    findAllGroups(){
      //调用服务
      let that = this;
      this.$axios
        .get("group/findAllGroup")
        .then(res => {
          let data = res.data;
          if (data.code === 0) {
            this.allGroups = data.data;
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(function(error) {
          console.log("axios请求异常:" + error);
          that.$message.error("网路请求异常");
        });
    }
    ,save(){
      //调用服务
      let that = this;
      this.$refs["groupForm"].validate(valid => {
        if (valid) {
          this.$axios
            .get("group/save", {
              params: this.group
            })
            .then(res => {
              let data = res.data;
              if (data.code === 0) {
                this.$message({ message: data.msg, type: 'success'});
                this.$emit('input',false);
                this.$emit('flush-data');
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(function(error) {
              console.log("axios请求异常:" + error);
               that.$message.error("网路请求异常");
            });
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
    
</style>
